﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FinalExam
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        public Form3(int id,string name,string people,int t)
        {
            this.id = id;
            txtBookName.Text = name;
            txtAuthor.Text = people.ToString();
            txtTime.Text = t.ToString();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        private int id;
        private void button1_Click(object sender, EventArgs e)
        {
            var peopleString = txtAuthor.Text.ToString();
            var tString = txtTime.Text.ToString();

            var name = txtBookName.Text;
            var people = peopleString.Count() > 0 ? int.Parse(peopleString) : 0;
            var t = tString.Count() > 0 ? int.Parse(tString) : 0;

            if (id > 0)
            {
                if (!string.IsNullOrEmpty(name))
                {
                    var sql = string.Format("update Articles set Title='{0}',Author='{1}',CreateTime={2}where Id={3}", name, people, t, this.id);
                    var res = DbHelp.AddOrUpdateOrDelete(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("修改成功", "提示");
                        this.Close();
                    }
                }
            }
            else
            {
               

                



                if (!string.IsNullOrEmpty(name))
                {
                    var sql = string.Format("insert into Articles (Title,Author,CreatedTime)values('{0}','{1}','{2}')", name, people, t);

                    var res = DbHelp.AddOrUpdateOrDelete(sql);
                    if (res ==1)
                    {
                        MessageBox.Show("添加成功", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    
                }
                else
                {
                    MessageBox.Show("作者姓名不能为空", "提示");
                    this.Close();
                }

            }

            
          
            
        }
    }
}
